package org.ceteca.explica.client.partners;

import org.apache.log4j.Logger;
import org.ceteca.explica.client.gui.common.MapViewer;
import org.ceteca.explica.core.common.CountryOT;
import org.ceteca.explica.core.common.StateOT;
import org.ceteca.explica.core.partners.AddressOT;
import org.ceteca.javaglade.Gui;
import org.ceteca.javaglade.handlers.GenericHandler;
import org.eclipse.swt.widgets.Event;








/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class AddressListener extends GenericHandler {
	private AddressClient addressClient;
	private Logger logger;
	
	public AddressListener() {
		super();
	}

	public AddressListener(AddressClient addressClient) {
		super();
		this.addressClient = addressClient;
		this.logger = addressClient.getLogger();
//		this.setJavaGlade(addressClient.getJGlade());
	}

	public AddressClient getAddressClient() {
		return addressClient;
	}

	public void setAddressClient(AddressClient addressClient) {
		this.addressClient = addressClient;
	}

	public Logger getLogger() {
		return logger;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void on_country_combo_changed(Event e) {
		CountryOT country = null;
		int selectedCountry = addressClient.getComboAddressCountry().getSelectionIndex();
		if (selectedCountry > -1)
			country = addressClient.getCountries().get(selectedCountry);
		if (country != null) {
			addressClient.loadStates(country);
			addressClient.getComboAddressState().removeAll();
			int selected = -1;
			for (int i = 0; i < addressClient.getStates().size(); i++) {
				StateOT f = addressClient.getStates().get(i);
				addressClient.getComboAddressState().add(f.getName());
				if (((AddressOT) addressClient.getSelectedEntity()).getState() != null)
					if (f.getIdState() == ((AddressOT) addressClient.getSelectedEntity())
							.getState().getIdState()) {
						selected = i;
						// break;
					}
			}
			if (selected > -1
					&& ((AddressOT) addressClient.getSelectedEntity()).getState() != null)
				addressClient.getComboAddressState().select(selected);

		}

	}
	
	public void on_button_map_view_clicked(Event e) {
		Gui parentGui = addressClient.getEditionGui();
		
		MapViewer map=new MapViewer((AddressOT)addressClient.getSelectedEntity(), addressClient, logger);
		map.show();
	}
}
